import sys
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
import hashlib
import base64
import hmac
import time
from urllib.parse import quote_plus


class DingDingUtils:


    def dingdingUrl(self):
        timestamp = str(round(time.time() * 1000))
        url = 'https://oapi.dingtalk.com/robot/send?access_token=7bc13df05742cbf38a2dd6689eaf0c24ab95635be71d97f509cb462c176f2ad1'
        secret = 'SEC81fb71003357ea218c54b1c8966381bf2ecad574b3d83671f28251e39df766a0'
        secret_enc = secret.encode('utf-8')
        string_to_sign = '{}\n{}'.format(timestamp, secret)
        string_to_sign_enc = string_to_sign.encode('utf-8')
        hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
        sign = quote_plus(base64.b64encode(hmac_code))

        webhook = url + '&timestamp=' + timestamp + '&sign=' + sign


        # print(webhook)


        return webhook